package de.medizin.uni.halle.irm;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class QuestionSettingsFragment extends SherlockFragment{
	private SQLiteDatabase database;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View v = (LinearLayout)inflater.inflate(R.layout.questionsettings, container, false); 
		
		((Button) v.findViewById(R.id.store_question)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View ve) {
				store_question(v);				
			}
		});		
		((Button) v.findViewById(R.id.add_answer)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View ve) {
				add_answer(v, ((EditText)v.findViewById(R.id.question_id)).getText().toString(),
						((EditText)v.findViewById(R.id.question)).getText().toString());				
			}
		});
		((Button) v.findViewById(R.id.reset)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View ve) {
				((EditText)v.findViewById(R.id.question_id)).setText("");
				((EditText)v.findViewById(R.id.question)).setText("");	
				updateanswerlist(v, null);
				((Button) v.findViewById(R.id.add_answer)).setVisibility(View.INVISIBLE);
			}
		});
		
		updatequestionlist(v);
		
		return v;	        
	}
    
	private void updatequestionlist(final View v) {
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);	
    	int[] to = new int[] { R.id.list_question_id, R.id.list_question, R.id.list_token ,R.id.list_position ,R.id.list_state };
        String[] fields = new String[] {"_id", "question", "token", "position", "state"};
    	final Cursor c = database.rawQuery("SELECT _id, question, token, position, state FROM questions", null);
    	CursorAdapter data = new SimpleCursorAdapter(getSherlockActivity(), R.layout.question_list, c, fields, to, 0 );
    	ListView lv  = (ListView) v.findViewById(R.id.questions);
    	lv.setAdapter(data);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				v.findViewById(R.id.add_answer).setVisibility(View.VISIBLE);
				c.moveToPosition(position);
				((EditText) v.findViewById(R.id.token)).setText(c.getString(c.getColumnIndex("token")));
				((EditText) v.findViewById(R.id.position)).setText(c.getString(c.getColumnIndex("position")));
				if(c.getString(c.getColumnIndex("state")).equals("active"))
					((CheckBox) v.findViewById(R.id.state)).setChecked(true);
				else 
					((CheckBox) v.findViewById(R.id.state)).setChecked(false);
				((EditText) v.findViewById(R.id.question)).setText(c.getString(c.getColumnIndex("question")));
				((EditText) v.findViewById(R.id.question_id)).setText(((TextView) view.findViewById(R.id.list_question_id)).getText());
				updateanswerlist(v, ((TextView) view.findViewById(R.id.list_question_id)).getText().toString());
			}
		});
	}
	protected void updateanswerlist(View v, String id) {
		database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);	
    	int[] to = new int[] { R.id.list_answer_id, R.id.list_answer, R.id.list_answer_token };
        String[] fields = new String[] {"_id","answer","token","question_id"};
    	Cursor c = database.rawQuery("SELECT * FROM answers WHERE question_id=" +id+";", null);
    	CursorAdapter data = new SimpleCursorAdapter(getSherlockActivity(), R.layout.answer_list, c, fields, to, 0 );
    	ListView lv  = (ListView) v.findViewById(R.id.answers);
    	lv.setAdapter(data);		
	}

	protected void add_answer(final View v, final String id, String question) {
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
    	final Dialog dialog = new Dialog(getSherlockActivity());
    	dialog.setContentView(R.layout.add_answer_dialog);
    	dialog.setTitle(question);
    	dialog.setCanceledOnTouchOutside(true);
    	dialog.show();
    	((Button) dialog.findViewById(R.id.add_answer)).setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v2) {
				ContentValues answer = new ContentValues();
				answer.put("question_id", id);
				answer.put("answer",((EditText)dialog.findViewById(R.id.answer)).getText().toString());
				answer.put("token",((EditText)dialog.findViewById(R.id.answer_token)).getText().toString());
				database.insert("answers", null, answer);
				updateanswerlist(v, id);
				dialog.cancel();
			}
		});
    	((Button) dialog.findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}
	protected void store_question(View v) {
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);

    	String id = ((EditText)v.findViewById(R.id.question_id)).getText().toString();
    	ContentValues q = new ContentValues();
    	q.put("question", ((EditText)v.findViewById(R.id.question)).getText().toString());
    	q.put("position", ((EditText)v.findViewById(R.id.position)).getText().toString());
    	q.put("token", ((EditText)v.findViewById(R.id.token)).getText().toString());
    	if(((CheckBox)v.findViewById(R.id.state)).isChecked()){
    		Toast.makeText(getSherlockActivity(), "CHECKED", Toast.LENGTH_LONG).show();
    		q.put("state", "active");
    	}
    	else{
    		Toast.makeText(getSherlockActivity(), "UNCHECKED", Toast.LENGTH_LONG).show();
    		q.put("state", "inactive");	
    	}
    	if(id.equals("")){
    		database.insert("questions", null, q);
    		Cursor last_question=database.rawQuery("SELECT * FROM questions ORDER BY _id DESC LIMIT 1;", null);
    		last_question.moveToFirst();
    		((EditText)v.findViewById(R.id.question_id)).setText(last_question.getString(last_question.getColumnIndex("_id")));
    	}
    	else
    		database.update("questions", q, "_id="+id, null);
    	updatequestionlist(v);
    	((Button) v.findViewById(R.id.add_answer)).setVisibility(View.VISIBLE);
	}    
}
